#encoding:utf-8
import urllib
import json

class netdisk():
    '''a class which can fetch 115 netdisk`s file though the given url or
        pickcode
        the request url examplehttp://uapi.115.com/?ct=upload_api&ac=
        get_pick_code_info&pickcode=aqbc8e20&version=1175"'''
    api = "http://uapi.115.com/?ct=upload_api&ac=get_pick_code_info\
&pickcode=%s&version=1175"
    def __init__(self,code):
        '''code is either URL of the file or pickcode'''
        self.__code = code
        f = urllib.urlopen(self.api %self.__code)
        data = f.read()
        self.__data = json.loads(data)
        self.state = self.__data['State']
        self.message = urllib.unquote(self.__data['Message'])
        if self.state == True:
            self.Sha1 = self.__data['Sha1']
            self.filename = urllib.unquote(self.__data['FileName'])
            self.urls = [url['Url'] for url in self.__data["DownloadUrl"]]
    def json(self):
        '''return a json str of the file`s info'''
        if self.state == True:
            return json.dumps({'filename':self.filename,
                               'sha':self.Sha1,
                               'urls':self.urls,
                               'state':True
                               })
        else:
            return json.dumps({'state':False,
                            'message':self.message})


#simple unit test
if __name__=="__main__":
    a = netdisk("aqbc8e20")
    print a._netdisk__data
    print a.filename
    print a.json()



        
